//
//  FindHeadCell.m
//  WOQIEarjoy
//
//  Created by 孙鹏飞 on 2023/11/24.
//  Copyright © 2023 WOQI. All rights reserved.
//

#import "FindHeadCell.h"
#import "SDCycleScrollView.h"
#import "BannerModel.h"

#import <BIToolModule/BIProjectDefine.h>

@interface FindHeadCell()<SDCycleScrollViewDelegate>

@property (nonatomic , strong)SDCycleScrollView *cycleView;
@property(nonatomic,strong) NSMutableArray * dataSource;

@end


@implementation FindHeadCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
//        self.backgroundColor = [UIColor clearColor];
//        [self.cycleView adjustWhenControllerViewWillAppera]; //解决viewWillAppear时出现时轮播图卡在一半的问题

    }
    return self;
}
- (void)setUI{
    self.cycleView.frame = CGRectMake(20, 0, BIScreenWidth - 40, 200);
    [self.contentView addSubview:self.cycleView];
    
}
-(void)setImageHight:(float)imageHight{
    _imageHight = imageHight;
    NSLog(@"---ssd--%f",self.imageHight);
    self.cycleView.frame = CGRectMake(20, 0, BIScreenWidth - 40, self.imageHight);
}

-(void)setModelArray:(NSMutableArray *)modelArray{
    _modelArray = modelArray;
    [self.dataSource  removeAllObjects];
    for (int i=0; i<_modelArray.count; i++) {
        BannerModel *model = [_modelArray objectAtIndex:i];
        NSString * url = model.image;
        [self.dataSource addObject:url];
    }
//    NSLog(@"--------%@",self.dataSource);

    
    self.cycleView.imageURLStringsGroup = self.dataSource;
}

/** 点击banner图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
    
    NSLog(@"--------%ld",(long)index);
    BannerModel *model = [_modelArray objectAtIndex:index];
    [self jumpUrl:model.redirectPath];

//    NSLog(@"--------%@",model.redirectPath);

    NSDictionary * dic = @{@"ids":model.ids};
//    [MobClick event:@"click_find_banner" attributes:dic];

}
- (SDCycleScrollView*)cycleView{
    if (!_cycleView) {
        _cycleView = [[SDCycleScrollView alloc]init];
        _cycleView.autoScrollTimeInterval = 3;
        _cycleView.layer.cornerRadius = 10;
        _cycleView.layer.masksToBounds = YES;
//        _cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _cycleView.delegate = self;
    }
    return _cycleView;
}
- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(void)jumpUrl:(NSString *)url{
    if ([url rangeOfString:@"jd.com"].location != NSNotFound) {
        
        NSString * jdUrl =[[NSString stringWithFormat:@"openapp.jdmobile://virtual?params={\"category\":\"jump\",\"des\":\"m\",\"url\":\"%@\",\"keplerID\":\"0\",\"keplerFrom\":\"1\",\"kepler_param\":{\"source\":\"kepler-open\",\"otherData\":{\"mopenbp7\":\"0\"}},\"union_open\":\"union_cps\"}",url] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"openapp.jdmobile://"]]) {
//            NSLog(@"安装了京东");
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:jdUrl] options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }else if ([url rangeOfString:@"taobao.com"].location != NSNotFound) {
        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tbopen"];
        NSURL *tbUrl = [NSURL URLWithString:str];
        
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tbopen://"]]) {
            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
        }else{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
        }

    }
//    else if ([url rangeOfString:@"tmall.com"].location != NSNotFound) {
//        NSString *str = [url stringByReplacingOccurrencesOfString:@"https" withString:@"tmall"];
//        NSURL *tbUrl = [NSURL URLWithString:str];
//        if ([[UIApplication sharedApplication] canOpenURL:tbUrl]) {
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }else{
//            //直接跳转html
//            [[UIApplication sharedApplication] openURL:tbUrl options:@{} completionHandler:nil];
//        }
//    }
    else{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler:nil];
    }
}
@end
